#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

#include "ResultNotebook.h"
#include "UIThread.h"

#include "wx/arrimpl.cpp"
WX_DEFINE_OBJARRAY(wxGridArray); 
WX_DEFINE_OBJARRAY(wxResultGridTableArray);

ResultNotebook::ResultNotebook(wxWindow* parent, wxWindowID id,
							   const wxPoint& pos, 
							   const wxSize& size)
							   :wxAuiNotebook(parent,id,pos,size,wxNB_TOP )
{
	SetArtProvider(new wxAuiSimpleTabArt);

	resultPanel = new wxWindow(this,wxID_ANY,wxDefaultPosition,wxDefaultSize);
	AddPage(resultPanel,wxT("Result"));
	message = new wxTextCtrl(this,wxID_ANY,wxEmptyString,wxDefaultPosition,wxDefaultSize,wxTE_MULTILINE | wxTE_RICH  );
	AddPage(message,wxT("Message"));
	txt_result = new Edit(this,wxID_ANY);
	AddPage(txt_result,wxT("Text"));
	resultPanel->SetFont(wxFont(8, wxFONTFAMILY_MODERN , wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false, _("Microsoft Sans Serif")));

	//m_mgr.SetManagedWindow(resultPanel);
}

ResultNotebook::~ResultNotebook()
{
    m_mgr.UnInit();
}

wxWindow* ResultNotebook::GetListCtrl()
{
	return resultPanel;
}

wxTextCtrl* ResultNotebook::GetTextCtrl()
{
	return message;
}


Edit* ResultNotebook::GetSQLEditor()
{
	return txt_result;
}

void ResultNotebook::BindResult(ResultArray& result,int sets_count)
{
	if ( m_mgr.GetManagedWindow() == NULL)
		m_mgr.SetManagedWindow(resultPanel);
	
	if ( resultArray.GetCount() > 0 ){
		resultArray.Clear();
	}
	if ( resultGridTableArray.GetCount() > 0 ){
		resultGridTableArray.Clear();
	}
	if ( gridArray.GetCount() > 0 ){
		for(unsigned int j = 0; j < gridArray.GetCount(); ++j ){
			m_mgr.DetachPane(gridArray[j]);
			delete gridArray[j];
		}
		gridArray.Clear();
	}

	
	//resultArray = result;
	resultPanel->DestroyChildren();


	message->SetValue(wxEmptyString);
	message->SetDefaultStyle(wxColor(wxT("Black")));

	int layer = 0;

	for(unsigned int i = 0;i < result.GetCount();++i){

		if ( sets_count == 0)
			SetSelection(1);
		if ( result[i].type == sets ){
			ResultGridTable* table = new ResultGridTable();
			table->SetAttrProvider(new wxGridCellAttrProvider);
			resultGridTableArray.Add(table);
			resultGridTableArray[i]->Bind(result[i].row,result[i].col,result[i].result,result[i].nullArray);

			wxGrid* grid = new wxGrid(resultPanel,wxID_ANY,wxDefaultPosition,wxSize(100,100));
			gridArray.Add(grid);
			gridArray[i]->SetRowLabelSize(50);
			gridArray[i]->SetColLabelSize(20);
			//gridArray[i]->SetLabelBackgroundColour(wxColour(250,250,250));
			gridArray[i]->SetLabelFont(wxFont(8, wxFONTFAMILY_SWISS , wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false, _("Microsoft Sans Serif")));
			gridArray[i]->SetTable(resultGridTableArray[i],true);
			if ( result[i].row <= 3000 )
				gridArray[i]->AutoSize();
			m_mgr.AddPane(gridArray[i],wxAuiPaneInfo().Center().CaptionVisible(false).Position(layer++));
			SetSelection(0);
		}
		if ( result[i].type == error ){
			message->SetDefaultStyle(wxTextAttr(*wxRED));
			SetSelection(1);
		}
		message->AppendText(result[i].msg);
	}
	m_mgr.Update();
	//resultArray.Clear();
}

ResultArray ResultNotebook::GetResults()
{	
	return resultArray;
}

void ResultNotebook::ClearMessageText()
{
	message->SetValue(wxEmptyString);
	message->SetDefaultStyle(wxColor(wxT("Black")));
}